package com.ysstech.kettle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysstech.common.entity.PageBean;
import com.ysstech.common.service.DicvalueService;
import com.ysstech.kettle.entity.JobDatasource;
import com.ysstech.kettle.enums.KettleEnum;
import com.ysstech.kettle.mapper.JobDatasourceMapper;
import com.ysstech.kettle.service.JobDatasourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-11
 */
@Service
public class JobDatasourceServiceImpl extends ServiceImpl<JobDatasourceMapper, JobDatasource> implements JobDatasourceService {
    @Autowired
    private DicvalueService dicValueService;

    /**
     * 查询资源库表列表信息
     *
     * @param jobDatasource
     * @param pageBean
     * @return
     * @throws Exception
     */
    public PageBean<JobDatasource> queryAllDatasource(JobDatasource jobDatasource, PageBean pageBean) throws Exception {
        QueryWrapper<JobDatasource> wrapper = this.getQueryWrapper(jobDatasource);
        IPage pageBalance = this.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), wrapper);
        this.switchedListCategory(pageBalance.getRecords());
        return new PageBean<>(pageBalance);
    }


    /**
     * 类型，机构等中文的转换
     *
     * @param list
     * @return
     * @throws Exception
     */
    public List<JobDatasource> switchedListCategory(List<JobDatasource> list) throws Exception {
        Map<String, String> map = dicValueService.getCacheDicValueMap(KettleEnum.KETTLE_DATABASE_TYPE.getCode());
        for (JobDatasource jobDatasource : list) {
            if (null == jobDatasource) {
                continue;
            }
            if (!CollectionUtils.isEmpty(map) && map.containsKey(jobDatasource.getFtype())) {
                jobDatasource.setFtype(map.get(jobDatasource.getFtype()));
            }
        }
        return list;
    }

    /**
     * 查询条件
     *
     * @param jobDatasource
     * @return
     * @throws Exception
     */
    private QueryWrapper<JobDatasource> getQueryWrapper(JobDatasource jobDatasource) throws Exception {
        QueryWrapper<JobDatasource> wrapper = new QueryWrapper<>();
        if (null == jobDatasource) {
            return wrapper;
        }
        if (StringUtils.isNotBlank(jobDatasource.getName())) {
            wrapper.like("NAME", jobDatasource.getName());
        }
        if (StringUtils.isNotBlank(jobDatasource.getFtype())) {
            wrapper.eq("FTYPE", jobDatasource.getFtype());
        }
        wrapper.orderByAsc("NAME");
        return wrapper;
    }
}
